package com.sqlcheck.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.sqlcheck.util.JsonUtils;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.time.LocalDateTime;
import java.util.Map;

@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName(value = "database_connections")
public class DatabaseConnection {

    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    @TableField("connection_name")
    private String connectionName;

    @TableField("db_type")
    private DbType dbType;

    @TableField("host")
    private String host;

    @TableField("port")
    private Integer port;

    @TableField("database_name")
    private String databaseName;

    @TableField("username")
    private String username;

    @TableField("password_encrypted")
    private String passwordEncrypted;

    @TableField("connection_params")
    private String connectionParamsJson;

    @TableField("user_id")
    private Long userId;

    @TableField("is_default")
    private Boolean isDefault;

    @TableField("last_test_time")
    private LocalDateTime lastTestTime;

    @TableField("test_result")
    private Boolean testResult;

    @TableField(value = "created_at", fill = FieldFill.INSERT)
    private LocalDateTime createdAt;

    @TableField(value = "updated_at", fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updatedAt;

    @TableLogic
    @TableField("deleted")
    private Boolean deleted;

    // Helper methods for connection_params JSON field
    public Map<String, Object> getConnectionParams() {
        return JsonUtils.safeParseMap(this.connectionParamsJson);
    }

    public DatabaseConnection setConnectionParams(Map<String, Object> connectionParams) {
        this.connectionParamsJson = JsonUtils.safeToJsonString(connectionParams);
        return this;
    }

    public enum DbType {
        MYSQL, ORACLE, POSTGRESQL, SQLSERVER, GOLDENDB
    }
}